package app;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;

import models._MappingKit;

public class DemoConfig extends JFinalConfig{

	static Prop prop;

	public static void main(String[] args) {
		UndertowServer.start(DemoConfig.class, 8000, true);
	}

	static void loadConfig() {
		if (prop == null) {
			prop = PropKit.useFirstFound("demo-config-pro.txt", "demo-config-dev.txt");
		}
	}

	public void configConstant(Constants me) {
		loadConfig();
		me.setDevMode(true);

		// 开启对 jfinal web 项目组件 Controller、Interceptor、Validator 的注入
		me.setInjectDependency(true);
		// 开启对超类的注入。不开启时可以在超类中通过 Aop.get(...) 进行注入
		me.setInjectSuperClass(true);
	}

	public void configRoute(Routes me) {
//		me.add("/hello", HelloController.class);
//		me.add("/welcome", WelcomeController.class);
//		me.add("/user", UserController.class);
		me.add(new ApplicationRoutes());
		me.add(new AdminRoutes());
	}

	public void configEngine(Engine me) {}
	//    public void configPlugin(Plugins me) {}

	public void configPlugin(Plugins me) {
		
    	DruidPlugin druidPlugin = new DruidPlugin(prop.get("jdbcUrl"), prop.get("user"), prop.get("password").trim());
		me.add(druidPlugin);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		//only for Sqlite, if use mysql it doesn't need
		arp.setTransactionLevel(8);
		// 所有映射在 MappingKit 中自动化搞定
		_MappingKit.mapping(arp);
		me.add(arp);
		
		// 用于缓存bbs模块的redis服务
	    RedisPlugin jfinalRedis = new RedisPlugin("fujing", "127.0.0.1",6379);
	    me.add(jfinalRedis);
	}
	
	public static DruidPlugin createDruidPlugin() {
		loadConfig();
		return new DruidPlugin(prop.get("jdbcUrl"), prop.get("user"), prop.get("password").trim());
	}


	public void configInterceptor(Interceptors me) {}
	public void configHandler(Handlers me) {}

}
